from common.pandas_helper import PandasHelper
from common.utils import get_lastyear_mouth
from k3_data_report.business.cgtj_db import Cgtj
from k3_data_report.business.config import bussiness_cal_fields
from k3_data_report.finance.cpbfy_db import CpbFy
from k3_data_report.finance.xsbfy_db import XsbFy
from common.logger_helper import g_wlogger
__author__ = "Ypf"
_date_ = "2019/8/30-14:53"


def xsbfy(time1):
    db_sql = bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["insert_check_sql"] % ('k3skjllr', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic) == 0:
        xsb_sql=db_sql["xsbfyhs_jd_sql"]
        cpb_sql = db_sql["cpbfyhs_jd_sql"]
        insert_sql=db_sql["xsbfyhs_jd_isnert"]
        xs_name=['卜青伟', '曹明会','陈旭','崔涛','高建华','郭博文','侯磊','胡小军',  '李振刚','栾成举','瞿浩','孙柏荣','汪世来','叶元','周秀丽']
        dic_xsch = {}
        dic_fybx = {}
        for name in xs_name:
            jd_dic = CpbFy.get_jd_dic(cpb_sql, name)
            dic_fybx.update(jd_dic)
            jd_dic=XsbFy.get_xsjd_dic(xsb_sql,name)
            dic_xsch.update(jd_dic)
        for key2 in dic_xsch:
            dic_xsch[key2]=dic_xsch[key2]+dic_fybx[key2]
        #     q1sjch,q2sjch,q3sjch,q4sjch,q1sjfy,q2sjfy,q3sjfy,q4sjfy,
        XsbFy.insert_xsch(insert_sql,dic_xsch,time1)
    else:
        g_wlogger.winfo(f"xsbfy-{time1}:target_full_df, all data is empty.")

def create_df_xsbfy(time1):
    time2 = get_lastyear_mouth(time1)
    yea_2 = time2.split('-')[0]
    yea = time1.split('-')[0]
    db_sql = bussiness_cal_fields['k3_caozuo']
    xsb_sql = db_sql["xsbfyhs_jd_sql"]
    cpb_sql = db_sql["cpbfyhs_jd_sql"]
    xs_name = ['卜青伟', '曹明会', '陈旭', '崔涛', '高建华', '郭博文', '侯磊', '胡小军', '李振刚', '栾成举', '瞿浩', '孙柏荣', '汪世来', '叶元',
               '周秀丽']
    cal_dic_xsch = {}
    dic_fybx = {}
    for name in xs_name:
        jd_dic = CpbFy.get_jd_dic(cpb_sql, name)
        dic_fybx.update(jd_dic)
        jd_dic = XsbFy.get_xsjd_dic(xsb_sql, name)
        cal_dic_xsch.update(jd_dic)
    for key2 in cal_dic_xsch:
        cal_dic_xsch[key2] = cal_dic_xsch[key2] + dic_fybx[key2]
    #     q1sjch,q2sjch,q3sjch,q4sjch,q1sjfy,q2sjfy,q3sjfy,q4sjfy,
    list_out = []
    for key in cal_dic_xsch:
        dic_xsch = cal_dic_xsch.get(key, None)
        if dic_xsch == None:
            continue
        list_ap = [key, "客户经理", "", "", "", "", dic_xsch[0], "", "",
                   dic_xsch[4], "", dic_xsch[1], "", "",
                   dic_xsch[5], "", dic_xsch[2], "", "",
                   dic_xsch[6], "", dic_xsch[3], "", "",
                   dic_xsch[7],
                   float(dic_xsch[4]) + float(dic_xsch[5]) + float(dic_xsch[6]) + float(dic_xsch[7]), "",
                   ""]
        list_out.append(list_ap)
    #     q1sjch,q2sjch,q3sjch,q4sjch,q1sjfy,q2sjfy,q3sjfy,q4sjfy
    columns = ["人员", "职位", f"{yea}年业绩指标", f"{yea_2}年实际出库", "预计有效毛利率", "Q1计划出货", "Q1实际出货", "任务完成率", "Q1预算费用",
               "Q1实际费用", "Q2计划出货", "Q2实际出货", "任务完成率", "Q2预算费用",
               "Q2实际费用", "Q3计划出货", "Q3实际出货", "任务完成率", "Q3预算费用",
               "Q3实际费用", "Q4计划出货", "Q4实际出货", "任务完成率", "Q4预算费用",
               "Q4实际费用", "年度实际费用", "与预算费用差额", "年度任务完成率"]
    k3cpbfy_df = PandasHelper.create_dataframe(data=list_out, columns=columns)
    return k3cpbfy_df

def xsbfy_main(month):
    try:
        xsbfy(month)
        g_wlogger.winfo(f"销售部费用月数据计算完成{month}！")
        return True
    except Exception as e:
        g_wlogger.werror(f"xsbfy_main has error {e} with {month}")
        print(f"xsbfy_main has error:{e} with:{month}")
        return False

